package com.blackbird.jang.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "BLACKBIRD_DB";
	
	private static final String TABLE_SCORE = "USER_SCORES";
	
	private static final String INITIAL_SQL = "CREATE TABLE USER_SCORES (NAME VARCHAR(50), SCORE INTEGER);";
	
	
	private static final int DB_VERSION = 1;

	private DBHelper(Context context, String dbName, CursorFactory factory,
			int version) {
		super(context, dbName, factory, version);
	}

	public DBHelper(Context context) {
		this(context, DB_NAME, DB_VERSION);
	}

	public DBHelper(Context context, String dbName) {

		this(context, dbName, DB_VERSION);
	}

	public DBHelper(Context context, String dbName, int version) {

		this(context, dbName, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {		
		System.out.println("#################################");
		
		db.execSQL(INITIAL_SQL);
		
		ContentValues values = new ContentValues();
		values.put("NAME", "default");
		values.put("SCORE", 1000);
		int resultRow = (int)db.insert(TABLE_SCORE, null, values);
		
		System.out.println("insert record number is "+ resultRow);
		db.close();
	}

	/**
	 * 查询用户得分
	 * @param userName
	 * @return
	 */
	public int queryScore(String userName) {
		
		if(userName == null || userName.trim().equals("")){
			userName = "default";
		}

		int result = 0;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_SCORE,
				new String[] { "NAME", "SCORE" }, "NAME=?",
				new String[] { userName }, null, null, null);

		while (cursor.moveToNext()) {			
			result += cursor.getInt(cursor.getColumnIndex("SCORE"));
		}
		
		cursor.close();
		db.close();
		
		return result;
	}

	
	/**
	 * 更新用户得分
	 * @param user
	 * @param score
	 */
	public int updateScore(String user, int score) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("NAME", user);
		values.put("SCORE", score);
		
		String where = "NAME=?";
        String[] whereValue={Integer.toString(score)};		
		
		int resultRow = db.update(TABLE_SCORE, values, where, whereValue);
		
		db.close();
		
		
		return resultRow;

	}
	
	public int insert(String user, int score) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("NAME", user);
		values.put("SCORE", score);
		int resultRow = (int)db.insert(TABLE_SCORE, null, values);
		
		db.close();
	
		return resultRow; 
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		// String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
		// db.execSQL(sql);
		// onCreate(db);
	}

}
